﻿Imports System.Windows.Forms
Imports System.Drawing

<Serializable()> Public Class RectangleTool
    Inherits BaseTool

    Dim dp1 As New Point


    Dim dp2 As New Point
    Dim dp3 As New Point
    Dim dp4 As New Point

    Dim dp2_old As New Point
    Dim dp3_old As New Point
    Dim dp4_old As New Point

    Dim x1 As Double
    Dim y1 As Double
    Dim x2 As Double
    Dim y2 As Double
    Dim x3 As Double
    Dim y3 As Double
    Dim k As Double
    Dim kv As Double


    Dim lineRelation As Integer = 0 '1 x2=x1 2 y2=y1 3 normal


    'Dim k As Double

    Dim pt As New Point
    Dim Xlength As Integer
    Dim Ylength As Integer



    Public Overrides Sub mouseDblClick(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Public Overrides Sub mouseDown(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseDownEvent)
        If e.button = 1 Then



            If Me.OperShapeProperty Is Nothing Then '第一次绘制



                Me.OperShapeProperty = ShapeFactory.getNewLineShape()


                Me.OperShapeProperty.P1Property = Me.DownPointProperty
                Me.OperShapeProperty.plist.Add(Me.DownPointProperty)

                dp1 = Me.DownPointProperty

                dp4_old = Me.DownPointProperty


            Else

                '绘制第二个点的情况
                If Me.OperShapeProperty.plist.Count = 1 Then

                    Me.OperShapeProperty.plist.Add(Me.DownPointProperty)

                    ControlPaint.DrawReversibleLine(getScreen(Me.OperShapeProperty.plist.Item(0)), getScreen(Me.DownPointProperty), lColor)

                    dp2_old = Me.DownPointProperty
                    dp2 = Me.DownPointProperty
                    dp3_old = Me.DownPointProperty


                    x1 = Convert.ToDouble(Me.OperShapeProperty.plist.Item(0).X)
                    y1 = Convert.ToDouble(Me.OperShapeProperty.plist.Item(0).Y)
                    x2 = Convert.ToDouble(Me.OperShapeProperty.plist.Item(1).X)
                    y2 = Convert.ToDouble(Me.OperShapeProperty.plist.Item(1).Y)


                    '首先要看是否垂直坐标轴
                    If (x2 = x1 And y2 <> y1) Then
                        lineRelation = 1

                    End If

                    If (y2 = y1 And x2 <> x1) Then

                        lineRelation = 2

                    End If


                    If x1 <> x2 And y1 <> y2 Then


                        lineRelation = 3

                        '斜率，与垂直线的斜率已经取得




                        k = (y2 - y1) / (x2 - x1)

                        kv = (-1) * (x2 - x1) / (y2 - y1)


                    End If



                Else

                    '绘制第三个点以及第四个点的情况
                    If Me.OperShapeProperty.plist.Count = 2 Then




                        Me.OperShapeProperty.plist.Clear()


                        Me.OperShapeProperty.plist.Add(dp1)

                        Me.OperShapeProperty.plist.Add(dp2)



                        Me.OperShapeProperty.plist.Add(dp3)

                        Me.OperShapeProperty.plist.Add(dp4)



                        InvokeEndDrawing()
                        Me.OperShapeProperty = Nothing

                    End If


                End If








            End If
        End If
    End Sub

    Public Overrides Sub mouseDrag(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseMoveEvent)

    End Sub

    Public Overrides Sub mouseMove(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseMoveEvent)
        If Me.OperShapeProperty.plist.Count = 1 Then

            ControlPaint.DrawReversibleLine(getScreen(Me.OperShapeProperty.plist.Item(0)), getScreen(Me.OldmovepointProperty), lColor)

            ControlPaint.DrawReversibleLine(getScreen(Me.OperShapeProperty.plist.Item(0)), getScreen(Me.NewmovepointProperty), lColor)

        End If


        If Me.OperShapeProperty.plist.Count = 2 Then
            pt.X = e.x
            pt.Y = e.y


            dp3.X = e.x
            dp3.Y = e.y


            '求两条垂直线的斜率方程y=kx+b，其中的B部分

            x3 = Convert.ToDouble(e.x)
            y3 = Convert.ToDouble(e.y)




            If lineRelation = 1 Then

                dp2.X = dp1.X
                dp2.Y = dp3.Y

                dp4.X = dp3.X
                dp4.Y = dp1.Y


            End If

            If lineRelation = 2 Then

                dp2.X = dp3.X
                dp2.Y = dp1.Y

                dp4.X = dp1.X
                dp4.Y = dp3.Y


            End If



            If (lineRelation = 3) Then




                Dim b1 As Double = y1 - k * x1
                Dim bv1 As Double = y3 - kv * x3


                '重新计算点P2,即求垂直交点
                dp2.X = (b1 - bv1) / (kv - k)
                dp2.Y = k * (b1 - bv1) / (kv - k) + b1


                '计算点P4





                Xlength = dp2.X - Me.OperShapeProperty.plist.Item(0).X
                Ylength = dp2.Y - Me.OperShapeProperty.plist.Item(0).Y

                dp4.X = dp3.X - Xlength
                dp4.Y = dp3.Y - Ylength

            End If





            '下面进入精彩绝伦，绕死你不偿命的正反线绘制阶段
            ControlPaint.DrawReversibleLine(getScreen(dp1), getScreen(dp2_old), lColor)

            ControlPaint.DrawReversibleLine(getScreen(dp1), getScreen(dp2), lColor)


            ControlPaint.DrawReversibleLine(getScreen(dp2_old), getScreen(dp3_old), lColor)

            ControlPaint.DrawReversibleLine(getScreen(dp2), getScreen(dp3), lColor)


            ControlPaint.DrawReversibleLine(getScreen(Me.OperShapeProperty.plist.Item(0)), getScreen(dp4_old), lColor)

            ControlPaint.DrawReversibleLine(getScreen(Me.OperShapeProperty.plist.Item(0)), getScreen(dp4), lColor)


            ControlPaint.DrawReversibleLine(getScreen(dp3_old), getScreen(dp4_old), lColor)

            ControlPaint.DrawReversibleLine(getScreen(dp3), getScreen(dp4), lColor)





            '刷新旧有坐标啦

            dp2_old = dp2
            dp3_old = dp3
            dp4_old = dp4






        End If
    End Sub

    Public Overrides Sub mouseUp(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_MouseUpEvent)

    End Sub

    Public Overrides Sub setself()

    End Sub

    Public Overrides Sub unSet()

    End Sub
End Class
